﻿namespace YidanSoft.Library.EditorUtility.CommandBars
{
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    using YidanSoft.Library.EditorUtility.Collections;
    using YidanSoft.Library.EditorUtility.Menus;
    using YidanSoft.Library.EditorUtility.WinControls;

    public class ChevronMenu : PopupMenu
    {
        private ToolBarItemCollection items = new ToolBarItemCollection();

        private void AddSubMenu(MenuCommand parentMenuCommand, MenuItem[] items)
        {
            for (int i = 0; i < items.Length; i++)
            {
                MenuItemEx realOwner = (MenuItemEx) items[i];
                MenuCommand command = new MenuCommand(realOwner.Text, realOwner.Icon, realOwner.Shortcut, realOwner.ClickHandler, realOwner);
                parentMenuCommand.MenuCommands.Add(command);
                if (realOwner.MenuItems.Count > 0)
                {
                    this.AddSubMenu(command, realOwner.MenuItems);
                }
            }
        }

        private void AddSubMenu(MenuCommand parentMenuCommand, Menu.MenuItemCollection items)
        {
            for (int i = 0; i < items.Count; i++)
            {
                MenuItemEx realOwner = (MenuItemEx) items[i];
                MenuCommand command = new MenuCommand(realOwner.Text, realOwner.Icon, realOwner.Shortcut, realOwner.ClickHandler, realOwner);
                parentMenuCommand.MenuCommands.Add(command);
                if (realOwner.MenuItems.Count > 0)
                {
                    this.AddSubMenu(command, realOwner.MenuItems);
                }
            }
        }

        private void Attach()
        {
            base.MenuCommands.Clear();
            foreach (ToolBarItem item in this.items)
            {
                string text = item.Text;
                switch (text)
                {
                    case string.Empty:
                    case null:
                        text = item.ToolTip;
                        break;
                }
                if (item.Style == ToolBarItemStyle.Separator)
                {
                    text = "-";
                }
                if (item.ComboBox != null)
                {
                    base.MenuCommands.Add(new MenuCommand(item.ComboBox));
                    item.ComboBox.Visible = true;
                    ComboBoxBase comboBox = (ComboBoxBase) item.ComboBox;
                    comboBox.ToolBarUse = false;
                }
                else
                {
                    MenuCommand command = new MenuCommand(text, (Bitmap) item.Image, (Shortcut) item.Shortcut, item.ClickHandler, item);
                    base.MenuCommands.Add(command);
                    if (item.MenuItems != null)
                    {
                        this.AddSubMenu(command, item.MenuItems);
                    }
                }
            }
        }

        public ToolBarItemCollection Items
        {
            get
            {
                return this.items;
            }
            set
            {
                this.items = value;
                this.Attach();
            }
        }
    }
}

